﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using System.Text;

namespace monitor_2_DBwork
{
    public class DBUpdater
    {
        public static bool RTPSInsert(string csvPath, out int count)
        {
            count = 0;
            string[] rtpsNamesList = File.ReadAllLines(csvPath, Encoding.UTF8);
            List<RTPS> rtpsList = new List<RTPS>();
            foreach (string rtps in rtpsNamesList)
            {
                int regId, blrId;
                string rtpsName;
                string[] rtpsInfo = rtps.Split(';');
                regId = int.Parse(rtpsInfo[0]);
                blrId = int.Parse(rtpsInfo[1]);
                rtpsName = rtpsInfo[2];
                var newRtps = new RTPS();
                newRtps.Idobl = regId;
                newRtps.Idblr = blrId;
                newRtps.Name = rtpsName;
                rtpsList.Add(newRtps);
                count++;
            }
            var rtpsRep = new DBRepository<RTPS>();
            return rtpsRep.AddRange(rtpsList)? true : false;
        }

        public static bool RegionsInsert(string csvPath, out int count)
        {
            count = 0;
            string[] regionsNamesList = File.ReadAllLines(csvPath, Encoding.UTF8);
            List<Oblast> regionsList = new List<Oblast>();
            foreach (string region in regionsNamesList)
            {
                string regName;
                regName = region;
                var newRegion = new Oblast();
                newRegion.Name = regName;
                newRegion.IdMainRTPS = null;
                regionsList.Add(newRegion);
                count++;
            }
            var regionsRep = new DBRepository<Oblast>();
            return regionsRep.AddRange(regionsList) ? true : false;
        }

        public static bool BLRInsert(string csvPath, out int count)
        {
            count = 0;
            string[] blrNamesList = File.ReadAllLines(csvPath, Encoding.UTF8);
            List<BLR> blrList = new List<BLR>();
            foreach (string blr in blrNamesList)
            {
                string blrName;
                blrName = blr;
                var newBlr = new BLR();
                newBlr.Name = blrName;
                blrList.Add(newBlr);
                count++;
            }
            var blrRep = new DBRepository<BLR>();
            return blrRep.AddRange(blrList) ? true : false;
        }

        //Обновление таблицы Access
        //public static bool AccessInsert(string csvPath, out int count)
        //{
        //    count = 0;
        //    string[] accessNamesList = File.ReadAllLines(csvPath, Encoding.UTF8);
        //    List<Access> accessList = new List<Access>();
        //    foreach (string access in accessNamesList)
        //    {
        //        string ru, us;
        //        string[] accessInfo = access.Split(';');
        //        us = accessInfo[0];
        //        ru = accessInfo[1];
        //        var newAccess = new Access();
        //        newAccess.enUS = us;
        //        newAccess.Name = ru;
        //        accessList.Add(newAccess);
        //        count++;
        //    }
        //    var accessRep = new DBRepository<Access>();
        //    return accessRep.AddRange(accessList) ? true : false;
        //}
    }
}
